//
//  HeroSegmentView.m
//  首页
//
//  Created by DYS on 15/12/28.
//  Copyright © 2015年 DYS. All rights reserved.
//

#import "HeroSegmentView.h"
#import "UIViewAdditions.h"

@interface HeroSegmentView ()<UIScrollViewDelegate>

@property (nonatomic,weak)UIScrollView *contentScrollView;
@property (nonatomic,weak)UIView *titleView;
@property (nonatomic,weak)UIViewController *parentViewController;
/** 标题按钮底部的指示器 */
@property (nonatomic, weak) UIView *indicatorView;
@property (nonatomic, weak) UIButton *selectedButton;
@property (nonatomic,strong)NSArray *viewControllers;
@end

@implementation HeroSegmentView

- (void)setViewControllers:(NSArray *)viewControllers owner:(UIViewController *)parentViewController
{
    self.viewControllers = viewControllers;
    self.parentViewController = parentViewController;
    UIScrollView * contentScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    contentScrollView.pagingEnabled = YES;
    contentScrollView.showsVerticalScrollIndicator = NO;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    contentScrollView.bounces = NO;
    contentScrollView.delegate = self;
    
    [self addSubview:contentScrollView];
    self.contentScrollView = contentScrollView;
    
    for (int i = 0; i < viewControllers.count; i++) {
        UIViewController *vc = viewControllers[i];
        [self.parentViewController addChildViewController:vc];
    }
    _contentScrollView.contentSize = CGSizeMake(_contentScrollView.frame.size.width * viewControllers.count, _contentScrollView.frame.size.height);
    
    [self addChildVcView];
}

-(UIView *)topControlViewWithFrame:(CGRect)frame
{
    UIView *titleView = [[UIView alloc] initWithFrame:frame];
 
    for (int i = 0; i < _viewControllers.count; i++) {
        UIViewController *vc = _viewControllers[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        CGFloat width = frame.size.width / _viewControllers.count;
        btn.frame = CGRectMake(i*width, 0, width, frame.size.height);
        [btn setTitle:vc.title forState:UIControlStateNormal];
        btn.tag = 100 + i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [titleView addSubview:btn];
    }
    
    UIButton *firstBtn = [titleView.subviews firstObject];
    
    UIView *indicatorView = [[UIView alloc] init];
    indicatorView.backgroundColor = [firstBtn titleColorForState:UIControlStateSelected];
    
    indicatorView.height = 1;
    indicatorView.top = titleView.height - indicatorView.height;
    // 立刻根据文字内容计算label的宽度
    //[firstBtn.titleLabel sizeToFit];
    indicatorView.width = firstBtn.titleLabel.width;
    indicatorView.centerX = firstBtn.centerX;
    [titleView addSubview:indicatorView];
    self.indicatorView = indicatorView;
    
    self.titleView = titleView;
    
    // 默认情况 : 选中最前面的标题按钮
    firstBtn.selected = YES;
    self.selectedButton = firstBtn;
    
    return titleView;
}

- (void)btnClick:(UIButton *)btn{
    
    self.selectedButton.selected = NO;
    btn.selected = YES;
    self.selectedButton = btn;
    
    // 指示器
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.width = btn.titleLabel.width;
        self.indicatorView.centerX = btn.centerX;
    }];
    [self contentScrollViewShowPage:btn.tag-100];
}

- (void)addChildVcView
{
    // 子控制器的索引
    NSUInteger index = _contentScrollView.contentOffset.x / _contentScrollView.frame.size.width;
    
    // 取出子控制器
    UIViewController *childVc = self.viewControllers[index];
    if ([childVc isViewLoaded]) return;
    
    childVc.view.frame = _contentScrollView.bounds;
    
    //NSLog(@"%@",NSStringFromCGRect(_contentScrollView.bounds));
    [_contentScrollView addSubview:childVc.view];
}

- (void)contentScrollViewShowPage:(NSInteger)page
{
    [_contentScrollView setContentOffset:CGPointMake(page * _contentScrollView.frame.size.width, 0) animated:YES];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    [self addChildVcView];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSUInteger index = scrollView.contentOffset.x / scrollView.width;
    
    UIButton *titleButton = self.titleView.subviews[index];
    [self btnClick:titleButton];

    [self addChildVcView];
}


@end
